package com.hc.music.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hc.music.common.R;
import com.hc.music.dao.UserMapper;
import com.hc.music.entity.SongList;
import com.hc.music.entity.User;
import com.hc.music.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public R addUser(User user) {
        if(userMapper.insert(user) > 0){
            return R.success("添加成功");
        }else {
            return R.error("添加失败");
        }
    }

    @Override
    public R updateUserMsg(User user) {
        int i = userMapper.updateById(user);
        if(i > 0){
            return R.success("更新成功");
        }
        return R.success("更新失败");
    }

    @Override
    public R updateUserAvator(MultipartFile file, int id) {
        try {
            deleteUserPic(id);
            String filename = System.currentTimeMillis() + file.getOriginalFilename();
            String path = System.getProperty("user.dir")+System.getProperty("file.separator")+"img"+System.getProperty("file.separator")+"userPic" +System.getProperty("file.separator")+ filename;
            File dest = new File(path);
            file.transferTo(dest);
            User singer = new User();
            singer.setId(id);
            String rp = "img/userPic/"+ filename;
            singer.setAvator(rp);
            if(userMapper.updateById(singer) > 0){
                return R.success("上传成功",rp);
            }else {
                return R.error("上传失败");
            }
        } catch (IOException e) {
            return R.error("上传失败");
        }
    }

    private void deleteUserPic(Integer id){
        String url = userMapper.selectById(id).getAvator();
        if(!url.contains("user.jpg")){
            String name = url.substring(12);
            String fPath = System.getProperty("user.dir")+System.getProperty("file.separator")+"img"+System.getProperty("file.separator")+"userPic"+System.getProperty("file.separator")+name;
            File fFile = new File(fPath);
            if(fFile.exists()){
                fFile.delete();
            }
        }
    }

    @Override
    public R updatePassword(User user) {
        int i = userMapper.updateById(user);
        if(i > 0){
            return R.success("密码重置成功");
        }
        return R.success("密码重置失败");
    }

    @Override
    public boolean existUser(String username) {
        return false;
    }

    @Override
    public R verityPasswd(String username, String password) {
        QueryWrapper<User> query = new QueryWrapper<>();
        query.eq("username",username);
        query.eq("password",password);
        List<User> userList = userMapper.selectList(query);
        if(userList.size() > 0){
            return R.success("登录成功",userList.get(0));
        }else {
            return R.error("登录失败");
        }
    }

    @Override
    public R deleteUser(Integer id) {
        if(userMapper.deleteById(id)>0){
            return R.success("删除成功");
        }else {
            return R.error("删除失败");
        }
    }

    @Override
    public R allUser() {
        return R.success(null,userMapper.selectList(null));
    }

    @Override
    public R userOfId(Integer id) {
        User user = userMapper.selectById(id);
        return R.success(null,user);
    }

    @Override
    public R loginStatus(User loginRequest, HttpSession session) {
        return null;
    }

    @Override
    public R loginEmailStatus(User user, HttpSession session) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email",user.getEmail());
        queryWrapper.eq("password",user.getPassword());
        List<User> userList = userMapper.selectList(queryWrapper);
        if(userList.size() > 0){
            return R.success("登录成功",userList.get(0));
        }else {
            return R.error("登录失败");
        }
    }

    @Override
    public User findByEmail(String email) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email",email);
        List<User> userList = userMapper.selectList(queryWrapper);
        if(!userList.isEmpty()){
            return userList.get(0);
        }
        return null;
    }

    @Override
    public R updatePassword01(User user) {
        User src = userMapper.selectById(user);
        if(src == null){
            return R.error("用户不存在");
        }else {
            String oldPassword = src.getOldPassword();
            //可以作加盐之类的处理,此处只演示
            if(!user.getOldPassword().equals(oldPassword)){
                return R.error("旧密码错误");
            }else {
                user.setOldPassword(user.getPassword());
                int i = userMapper.updateById(user);
                if(i > 0){
                    return R.success("密码修改成功");
                }else {
                    return R.error("密码修改失败");
                }
            }
        }
    }
}
